<?php
/* *
* $Author ：PHPYUN开发团队
*
* 官网: http://www.phpyun.com
*
* 版权所有 2009-2019 宿迁鑫潮信息技术有限公司，并保留所有权利。
*
* 软件声明：未经授权前提下，不得用于商业运营、二次开发以及任何形式的再次发布。
*/
class index_controller extends zphnetv_controller{
    function index_action(){
        
    }
    //查看是否存在账号
    function hasMem_action(){
        $post               =   str_replace('“','"',stripslashes($_POST['postdata']));
        
        $post               =   json_decode($post,true);
        
        $error              =   1;
        if($post['telphone']){
            $userinfoM      =   $this->MODEL('userinfo');

            $mem            =   $userinfoM->getInfo(array('moblie'=>$post['telphone']));
            if(empty($mem)){

                $error      =   2;
                $msg        =   '用户不存在'; 
            }else{
                $data['usertype'] =  $mem['usertype'];
                $data['uid']      =  $mem['uid'];
                if($mem['usertype']==1){

                    $resumeM      =   $this->MODEL('resume');

                    $returndata       =  $resumeM->getResumeRaw(array('telphone'=>$post['telphone']),array('all'=>1));
                    $data['resume']   =   $returndata['resume'];

                }else if($mem['usertype']==2 && $post['zphid']){

                    $zphnetM    =   $this->MODEL('zphnet');
                    $zcnum      =   $zphnetM->getZphnetComNum(array('uid'=>$mem['uid'],'zid'=>$post['zphid'],'status'=>1));
                    
                    $company    =   $zphnetM->getSyncCompanyData(array('uid' => array('in', pylode(',', $mem['uid'])), 'r_status' => '1', ));
                    $data['company']    =   $company[0];
                    $data['zcnum']      =   $zcnum;
                    
                }
            }
            //查询该源站用户简历信息
        }else{

            $error      =   2;
            $msg        =   '手机号不能为空';
        }
        $this->render_json($error,$msg,$data);
    }
    //查看是否存在简历
    function getResumeNum_action()
    {
        $post               =   str_replace('“','"',stripslashes($_POST['postdata']));
        
        $post               =   json_decode($post,true);
        
        $error              =   1;
        $msg                =   'ok';
        if($post['telphone']){
            $resumeM        =   $this->MODEL('resume');

            $resume         =   $resumeM->getResumeInfo(array('telphone'=>$post['telphone']),array('field'=>'uid'));
            $num            =   $resumeM->getExpectNum(array('uid'=>$resume['uid']));
            if($num<=0){

                $error      =   2;
                $msg        =   '简历不存在'; 
            }
            //查询该源站用户简历信息
        }else{

            $error      =   2;
            $msg        =   '手机号不能为空';
        }
        $this->render_json($error,$msg);
    }
    //获取简历
    function getResume_action(){
        $post               =   str_replace('“','"',stripslashes($_POST['postdata']));
        
        $post               =   json_decode($post,true);
        
        $error              =   1;
        $msg                =   'ok';
        if($post['telphone']){
            $resumeM		=   $this->MODEL('resume');
            $resumedata     =   $resumeM->getResumeRaw(array('telphone'=>$post['telphone']),array('all'=>1));
            if(!empty($resumedata['resume'])){

                if($resumedata['resume']['photo']){
                    $resumedata['resume']['photo']  =  checkpic($resumedata['resume']['photo']);
                }
                
                $data   =   $resumedata;

            }else{

                $error      =   2;
                $msg        =   '抱歉！暂无数据,请重试或创建简历'; 
                $data       =   $post;
            }
            //查询该源站用户简历信息
        }else{

            $error      =   2;
            $msg        =   '手机号不能为空';
            $data       =   $post;
            
        }
        $this->render_json($error,$msg,$data);
    }
    
    //同步平台简历
    function setResume_action(){
        // $postdata   =   file_get_contents("php://input");
        // $post       =   json_decode($postdata,true);
        $post               =   str_replace('“','"',stripslashes($_POST['postdata']));
        
        $post               =   json_decode($post,true);
        
        $error              =   1;

        
        if(!empty($post['resume'])){
            $resumeM        =   $this->MODEL('resume');
            $member         =   $post['member'];
            $resume         =   $post['resume'];
            $resume_expect  =   $post['resume_expect'];
            $resume_work    =   $post['resume_work'];
            $resume_edu     =   $post['resume_edu'];

            $cache          =   $this->MODEL('cache')->GetCache(array('user','city','job','hy','introduce'));
            $userdata       =   $cache['userdata'];
            $userclass_name =   $cache['userclass_name'];

            $resume['sex']  =   $resume['sex'] == '男'?1:2;
            //处理resume表的类别
            if($resume['edu']){
                
                $resume['edu']      =  $this->getCategoryId($resume['edu'],$userdata['user_edu'],$userclass_name);
            }
            if($resume['exp']){
                
                $resume['exp']      =  $this->getCategoryId($resume['exp'],$userdata['user_word'],$userclass_name);
            }
            if($resume['marriage']){
                
                $resume['marriage'] =  $this->getCategoryId($resume['marriage'],$userdata['user_marriage'],$userclass_name);
            }
            //处理resume_expect表的类别
            if(!empty($resume_expect)){
                $resume_expect['sex']          =   $resume_expect['sex'] == '男'?1:2;

                $resume_expect['exp']          =   $this->getCategoryId($resume_expect['exp'],$userdata['user_word'],$userclass_name);

                $resume_expect['edu']          =   $this->getCategoryId($resume_expect['edu'],$userdata['user_edu'],$userclass_name);

                $resume_expect['hy']           =   $this->getCategoryId($resume_expect['hy'],$cache['industry_index'],$cache['industry_name']);

                $resume_expect['report']       =   $this->getCategoryId($resume_expect['report'],$userdata['user_report'],$userclass_name);

                $resume_expect['type']         =   $this->getCategoryId($resume_expect['type'],$userdata['user_type'],$userclass_name);
                $resume_expect['jobstatus']    =   $this->getCategoryId($resume_expect['jobstatus'],$userdata['user_jobstatus'],$userclass_name);
                //处理职位类别id
                if ($resume_expect['job_classid'] ){
                    
                    $job_classname = @explode(',',$resume_expect['job_classid']);
                    
                    if(is_array($job_classname)){
                        
                        foreach($job_classname as $v){

                            $job_classid[]  =   array_search($v, $cache['job_name']);

                        }
                        $resume_expect['job_classid']  =  @implode(',',$job_classid);
                    }
                }
                //处理城市类别id
                if ($resume_expect['city_classid']){
                    
                    $city_classname = @explode(',',$resume_expect['city_classid']);
                    
                    if(is_array($city_classname)){
                        
                        foreach($city_classname as $v){
                            
                            $city_classid[]  =   array_search($v, $cache['city_name']);
                            
                        }
                        $resume_expect['city_classid']  =  @implode(',',$city_classid);
                    }
                }
            }
            //处理resume_edu表的类别
            if(!empty($resume_edu)){

                foreach($resume_edu as $ek=>$ev){

                    $resume_edu[$ek]['education']    =   $this->getCategoryId($ev['education'],$userdata['user_edu'],$userclass_name);

                }

            }

            $olduid     =   $member['web_uid'];
            $oldeid     =   $resume_expect['oldeid'];

            unset($member['web_uid']);
            unset($resume_expect['oldeid']);

            //插入数据 设计积分等数据，具体逻辑有待商榷
            $userinfoM = $this->MODEL('userinfo');

            if($olduid){
                $ismember   =   $userinfoM -> getInfo(array('uid'=>$olduid));
            }
            

            if(!$olduid || !$ismember['uid']){
                //插入member生产新uid，插入resume表
                $checkData = array(
                    'username' => $member['username'],
                    'moblie'   => $member['moblie'],
                );
                $memberCheck = $userinfoM->addMemberCheck($checkData);//检测用户名、手机号、邮箱是否已被注册
                
                if ($memberCheck['msg']){
                    $this->render_json(2,$memberCheck['msg']);
                }

                $ip    =  fun_ip_get();
                $time  =  time();
                $salt  =  substr(uniqid(rand()), -6);
                $pass  =  passCheck(rand(100000,999999),$salt);

                $mdata = array(
                    'username'  =>  $member['username'],
                    'password'  =>  $pass,
                    'usertype'  =>  1,
                    'salt'      =>  $salt,
                    'moblie'    =>  $member['moblie'],
                    'reg_date'  =>  $time,
                    'reg_ip'    =>  $ip,
                    'status'    =>  1
                );

                $resume['r_status'] = 0;
                
                $newuid  =  $userinfoM -> addInfo(array('mdata'=>$mdata,'udata'=>$resume,'sdata'=>array('integral'=>0)));
            }else{//有member 但可能有或没有resume表
                
                $resumeM -> addInfo(array('uid'=>$olduid,'rData'=>$resume,'utype'=>'admin'));

                $newuid =   $olduid;
            }

            
            $resume_expect['state'] = 0;
            $resume_expect['uid']   = $newuid;
            $resume_expect['source'] = 17;
            if(!$oldeid){
                //插入resume_expect表
                
                $return  =  $resumeM -> addInfo(array('uid'=>$newuid,'eData'=>$resume_expect,'utype'=>'admin'));

                $neweid     =  $return['id'];
                
            }else{
                $neweid     =  $oldeid;
                $resumeM -> upInfo(array('id'=>$neweid), array('eData'=>$resume_expect,'utype'=>'admin'));
                
            }

            $backarr        =   array();
            $backarr['eid'] =   $neweid;
            $backarr['uid'] =   $newuid;
            $newwork    =   array();
            $newedu     =   array();
            if(!empty($resume_work)){
                
                foreach($resume_work as $rwk=>$rwv){
                    $rwv['uid']     =   $newuid;
                    $rwv['eid']     =   $neweid;
                    $oldid          =   $rwv['oldid'];
                    $id             =   $rwv['id'];
                    unset($rwv['oldid']);
                    unset($rwv['id']);
                    if($oldid){
                        $resumeM -> addResumeWork($rwv,array('where'=>array('id'=>$oldid),'utype'=>'admin'));
                    }else{
                        $returnW    =   $resumeM -> addResumeWork($rwv,array('utype'=>'admin'));
                        $newwork[$id]    =   $returnW['id'];
                    }
                }
            }
            $backarr['work'] =   $newwork;
            if(!empty($resume_edu)){
                foreach($resume_edu as $rek=>$rev){
                    $rev['uid']     =   $newuid;
                    $rev['eid']     =   $neweid;
                    $oldid          =   $rev['oldid'];
                    $id             =   $rev['id'];
                    unset($rev['oldid']);
                    unset($rev['id']);
                    if($oldid){
                        $resumeM -> addResumeEdu($rev,array('where'=>array('id'=>$oldid),'utype'=>'admin'));
                    }else{
                        $returnE =   $resumeM -> addResumeEdu($rev,array('utype'=>'admin'));
                        $newedu[$id]    =   $returnE['id'];
                    }
                    
                }
            } 
            $backarr['edu'] =   $newedu;
            if($newuid){
                $error      =   1;
                $msg        =   'success';
                $data       =   $backarr;
            }else{
                $error      =   2;
                $msg        =   '同步失败';
            }
        }else{

            $error      =   2;
            $msg        =   '同步数据不存在';
            
        }
        $this->render_json($error,$msg,$data);
    }
    public function getCategoryId($data,$cacheidArr,$cacheName){

        $values  =   array_keys($cacheName,$data);
        if(count($values)>1){
            foreach($values as $k=>$v){
                if(in_array($v,$cacheidArr)){
                    $va    =   $values[$k];
                    break;
                }
            }
        }else{
            $va    =   $values[0];
        }

        $id  =  $va ? $va : 0;

        return $id;
    }
	//缓存提供接口
	public function zphnetCache_action(){
		
		//行业缓存
		$cache['industry']	=	$this -> obj -> select_all("industry",array('orderby'=>'sort,desc'));
		//职位缓存
		$cache['jobclass']	=	$this -> obj -> select_all("job_class",array('orderby'=>'sort,asc'),"`id`,`keyid`,`name`");
		//城市缓存
		$cache['cityclass']	=	$this -> obj -> select_all("city_class",array('orderby'=>'sort,asc'),"`id`,`keyid`,`name`");
		//企业分类缓存
		$cache['comclass']	=	$this -> obj -> select_all("comclass",array('orderby'=>'sort,asc'),"`id`,`keyid`,`name`,`variable`");
		//个人分类缓存
		$cache['userclass']	=	$this -> obj -> select_all("userclass",array('orderby'=>'sort,asc'),"`id`,`keyid`,`name`,`variable`");

		echo json_encode($cache);
	}
    
}
?>